//
//  ArticleCell.swift
//  iModern
//
//  Created by Ralf on 6/9/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit

class ArticleCell: TableViewCell
{
    let __arrow_view_tag                     = 9
    let __avatar_image_view_tag              = 10
    let __preview_image_view_tag             = 11
    let __number_of_favorites_image_view_tag = 12
    let __number_of_comments_image_view_tag  = 13
    let __play_image_view_tag                = 15

    let __alias_label_tag = 20

    let __number_of_favorites_button_tag = 21
    let __number_of_comments_button_tag  = 22

    let __message_textview_tag = 30

    override func awakeFromNib()
    {
        super.awakeFromNib()

        initAvatarImageView()
        initAliasLabel()
        initPreviewImageView()
        initNumberOfFavoritesImageView()
        initNumberOfFavoritesButton()
        initNumberOfCommentsImageView()
        initNumberOfCommentsButton()
    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)
    }

    func setAliasLabel(alias: String)
    {
        let label = self.viewWithTag(__alias_label_tag) as! UILabel
        label.text = alias
    }

    func setNumberOfFavoritesButton(numberOfFavorites: String)
    {
        let label = self.viewWithTag(__number_of_favorites_button_tag) as! UIButton
        label.setTitle(numberOfFavorites, forState: .Normal)
        label.contentHorizontalAlignment = .Left
        label.setTitleColor(UIColor.blackColor(), forState: .Normal)
    }

    func setNumberOfCommentsButton(numberOfComments: String)
    {
//        let label = self.viewWithTag(__number_of_comments_button_tag) as! UIButton
//        label.setTitle(numberOfComments, forState: .Normal)
//        label.contentHorizontalAlignment = .Left
//        label.setTitleColor(UIColor.blackColor(), forState: .Normal)
    }

    func setAvatarImageView(thumbnailURL: String?)
    {
        let imageView = self.viewWithTag(__avatar_image_view_tag) as! UIImageView
        if thumbnailURL != nil
        {
            let url = appDelegate.parameterManager.server + "/" + thumbnailURL!
            imageView.load(url, placeholder: nil)
        }
        else
        {
            imageView.image = UIImage(named: "Avatar-Default.png")
        }
    }

    func setPreviewImageView(imageURL: String)
    {
        let ivPreview = self.viewWithTag(__preview_image_view_tag) as! UIImageView
        ivPreview.load("\(appDelegate.parameterManager.server)/\(imageURL)", placeholder: nil)
    }

    func initAvatarImageView()
    {
        var imageView = self.viewWithTag(__avatar_image_view_tag) as? UIImageView
        if (imageView == nil)
        {
            let frame = CGRect(x: 10.0, y: 10.0, width: 40.0, height: 40.0)
            imageView = UIImageView(frame: frame)
            imageView!.image = UIImage(named: "Avatar-Default.png")
            imageView!.tag = __avatar_image_view_tag
            imageView!.layer.cornerRadius = 20.0
            imageView!.layer.masksToBounds = true
            imageView!.layer.borderColor = UIColor.blackColor().CGColor
            imageView!.layer.borderWidth = 1.0
            self.addSubview(imageView!)
        }
        
        var arrowImageView = self.viewWithTag(__arrow_view_tag) as? UIImageView
        if (arrowImageView == nil)
        {
            let frame = CGRect(x: screen.size.width-30, y: 20.0, width: 20.0, height: 20.0)
            arrowImageView = UIImageView(frame: frame)
            arrowImageView!.image = UIImage(named: "Next-60.png")
            arrowImageView!.tag = __arrow_view_tag
            self.addSubview(arrowImageView!)
        }
    }

    func initAliasLabel()
    {
        var label = self.viewWithTag(__alias_label_tag) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 60, y: 10, width: 100, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(16)
            label!.tag = __alias_label_tag
            self.addSubview(label!)
        }
    }

    func initPreviewImageView()
    {
        var imageView = self.viewWithTag(__preview_image_view_tag) as? UIImageView
        if (imageView == nil)
        {
            let frame = CGRect(x: 0, y: 60, width: screen.size.width, height: screen.size.width)
            imageView = UIImageView(frame: frame)
            imageView!.tag = __preview_image_view_tag
            imageView!.backgroundColor = UIColor.blackColor()
            self.addSubview(imageView!)
        }
    }

    func initNumberOfFavoritesImageView()
    {
        var imageView = self.viewWithTag(__number_of_favorites_image_view_tag) as? UIImageView
        if (imageView == nil)
        {
            let y     = 60 + screen.size.width + 10
            let frame = CGRect(x: 10, y: y, width: 20, height: 20)
            imageView = UIImageView(frame: frame)
            imageView!.image = UIImage(named: "Favorites-Small.png")
            imageView!.tag = __number_of_favorites_image_view_tag
            self.addSubview(imageView!)
        }
    }

    func initNumberOfFavoritesButton()
    {
        var button = self.viewWithTag(__number_of_favorites_button_tag) as? UIButton
        if (button == nil)
        {
            let x: CGFloat = 40
            let y          = 60 + screen.size.width + 10
            let frame      = CGRect(x: x, y: y, width: 40, height: 20)
            button = UIButton(frame: frame)
            button!.titleLabel!.font = UIFont.systemFontOfSize(12)
            button!.titleLabel!.text = "0"
            button!.tag = __number_of_favorites_button_tag
            self.addSubview(button!)
        }
    }

    func initNumberOfCommentsImageView()
    {
        var imageView = self.viewWithTag(__number_of_comments_image_view_tag) as? UIImageView
        if (imageView == nil)
        {
            let x: CGFloat = 90
            let y          = 60 + screen.size.width + 10
            let frame      = CGRect(x: x, y: y, width: 20, height: 20)
            imageView = UIImageView(frame: frame)
            imageView!.image = UIImage(named: "Comment-Small.png")
            imageView!.tag = __number_of_comments_image_view_tag
            self.addSubview(imageView!)
        }
    }

    func initNumberOfCommentsButton()
    {
        var button = self.viewWithTag(__number_of_comments_button_tag) as? UIButton
        if (button == nil)
        {
            let x: CGFloat = 120
            let y          = 60 + screen.size.width + 10
            let frame      = CGRect(x: x, y: y, width: 100, height: 20)
            button = UIButton(frame: frame)
            button!.titleLabel!.font = UIFont.systemFontOfSize(12)
            button!.titleLabel!.text = "0"
            button!.tag = __number_of_comments_button_tag
            self.addSubview(button!)
        }
    }

    func setMessageTextView(text: String)
    {
        var textView = self.viewWithTag(__message_textview_tag) as? UITextView
        if textView != nil
        {
            textView!.removeFromSuperview()
            textView = nil
        }
        let x      = 0
        let y      = 60 + screen.size.width + 10 + 20
        let height = self.bounds.height - y
        let frame  = CGRect(x: CGFloat(x), y: y, width: screen.width, height: height)
        textView = UITextView(frame: frame)
        textView!.tag = __message_textview_tag
        textView!.scrollEnabled = false
        textView!.userInteractionEnabled = false
        self.addSubview(textView!)

        let paragraphStyle = NSMutableParagraphStyle()

        paragraphStyle.lineHeightMultiple = 16
        paragraphStyle.maximumLineHeight = 20
        paragraphStyle.minimumLineHeight = 20

        paragraphStyle.alignment = .Left

        let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(14),
                          NSParagraphStyleAttributeName: paragraphStyle,
                          NSForegroundColorAttributeName: UIColor.darkGrayColor()]
        textView!.attributedText = NSAttributedString(string: text, attributes: attributes)
    }
}
